package com.bawei.module_vip.ui

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewModelScope
import com.bawei.lib_common.base.BaseResponse
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_net.storage.AuthMMKV
import com.bawei.module_vip.R
import com.bawei.module_vip.data.VipItem
import com.bawei.module_vip.databinding.FragmentVipBinding
import com.bawei.module_vip.ui.adapter.VipAdapter
import com.bawei.myclub.base.mvvm.view.BaseFragment
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@AndroidEntryPoint
class VipFragment : BaseFragment<VipViewModel, FragmentVipBinding>() {
    override val mLayoutResId: Int = R.layout.fragment_vip
    override val mViewModel: VipViewModel by viewModels()

    private val fixedUserId: Long = if (AuthMMKV.uId.isNotEmpty()) {
        AuthMMKV.uId.toLong()
    } else {
        1833
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mBinding = FragmentVipBinding.inflate(inflater, container, false)
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mViewModel.getVipById(fixedUserId)
        observeRequestEvent()
        mBinding.topVip.tvLogo.setOnClickListener {
            requireActivity().finish()
        }
    }

    private fun observeRequestEvent() {
        lifecycleScope.launch {
            mViewModel.requestEvent.collect { event ->
                when (event) {
                    Loading -> showLoading()
                    is Success -> showVipInfo()
                    is Failed -> showError(event.message)
                    Empty -> showEmpty()
                }
            }
        }
    }

    private fun showLoading() {
        // 显示加载 UI，比如进度条
        mBinding.progressBar.visibility = View.VISIBLE
    }

    private fun showVipInfo() {
        mBinding.vipItem= mViewModel.vipItem.value
        mBinding.progressBar.visibility = View.GONE
        mViewModel.vipItem.value.let { vip ->
            mBinding.tvDj.text = vip?.membershipLevel
            mBinding.tvTime.text = vip?.registrationDate
        }
    }

    private fun showError(message: String) {
        mBinding.progressBar.visibility = View.GONE
        Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show()
    }

    private fun showEmpty() {
        mBinding.progressBar.visibility = View.GONE
        Toast.makeText(requireContext(), "暂无 VIP 数据", Toast.LENGTH_SHORT).show()
    }
}